<?php

require('ac_db.inc.php');

//set error reporting
error_reporting(E_ERROR | E_WARNING);

//set credentials for database
setcredentials("tr2286","databases8620", "w4111b.cs.columbia.edu/ADB");

//create DB instance
$db = new Db("Proj1", "Part3");

//set email as permanent variable as it will be constantly used
$email;
$ticker;

//ensure that input is not blank which would mean that the site is illegally accessed
if(count($_POST) == 0){
	
	//if so, redirect to login
	echo "<meta http-equiv='refresh' content='0;url=http://localhost/login.php'>";
	exit;
}
else{
	
	//get ticker and email
	$email = $_POST['email'];
	$ticker = $_POST['ticker'];
	
	//call function to add to interest list
	addToInterest($email, $ticker, $db);
}

function addToInterest($email, $ticker, $db){
	
	//call db query to get current interest list
	$currentInterest = $db->getInterestStocks($email);
	
	if(!array_key_exists($ticker, $currentInterest)){
		
		//add to db
		$db->addInterest($email, $ticker);
		$db->closeSession();
		echo "<meta http-equiv='refresh' content='0;url=http://localhost/profile.php?email=$email'>";
		exit;
		//echo"added to db";
	}
	else{
		$db->closeSession();
		echo "<meta http-equiv='refresh' content='0;url=http://localhost/profile.php?email=$email'>";
		exit;
		//echo"already in db";
	}
}